package com.wanban.adutils.utils

import android.app.Activity
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants

/**
 * 插屏广告，全局一个实例，可以实现预加载
 */
object VideoInterstitialAdTools {

    private val videoAdTools by lazy { CommonAdTools() }

    fun loadAd(activity: Activity, callback: OnLoadAdCallback? = null){
        if (videoAdTools.isAdLoadSuccess()) {
            callback?.onAdLoaded()
            return
        }
        videoAdTools.loadAd(activity, AdConstants.AD_ID_VIDEO_DETAIL_INTERSTITIAL, object: OnLoadAdCallback(){
            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
                super.onAdLoaded(ad, *tag)
                callback?.onAdLoaded()
            }

            override fun onAdLoadFailed(errorCode: Int) {
                super.onAdLoadFailed(errorCode)
                callback?.onAdLoadFailed(errorCode)
            }

            override fun onAdClicked() {
                super.onAdClicked()
                callback?.onAdClicked()
            }

            override fun onAdOpened() {
                super.onAdOpened()
                callback?.onAdOpened()
            }

            override fun onAdComplete() {
                super.onAdComplete()
                callback?.onAdComplete()
            }
        })
    }

    fun isAdLoadSuccess(): Boolean{
        return videoAdTools.isAdLoadSuccess()
    }


    fun showAd(activity: Activity){
        if (activity.isFinishing || activity.isDestroyed) return
        if (!videoAdTools.isAdLoadSuccess()) {
            return
        }
        videoAdTools.showAdInActivity(activity)
    }

    fun release(){
        videoAdTools.release()
    }

}